layui.use(['table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var form = layui.form;
    /**
     * 自定义装修页面管理
     */
    var CustomPage = {
        tableId: "customPageTable"
    };

    /**
     * 初始化表格的列
     */
    CustomPage.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: 'id'},
            {
                field: 'pageType', sort: true, title: '页面类型', templet: function (d) {
                    return d.pageType == 1 ? "首页" : "自定义页"
                }
            },
            {field: 'pageName', sort: true, title: '页面名称'},
            {
                field: 'immediateOn', sort: true, title: '立即生效', templet: CustomPage.formatImmediateOn
            },
            {field: 'endTime', sort: true, title: '结束时间', width: 160},
            {field: 'startTime', sort: true, title: '开始时间', width: 160},
            {field: 'createUser', sort: true, title: '创建人'},
            {field: 'createTime', sort: true, title: '创建时间', width: 160},
            {field: 'updateUser', sort: true, title: '改变人'},
            {field: 'updateTime', sort: true, title: '改变时间', width: 160},
            {align: 'center', toolbar: '#tableBar', title: '操作', fixed: 'right', width: 200}
        ]];
    };

    /**
     * 点击查询按钮
     */
    CustomPage.search = function () {
        var queryData = getParam();

        table.reload(CustomPage.tableId, {
            where: queryData, page: {curr: 1}
        });
    };
    CustomPage.formatImmediateOn = function (d) {
        if (d.immediateOn == null) {
            return ""
        }
        let checkedStatus = d.immediateOn == 1 ? 'checked' : '';
        return "<input " + (d.pageType != 1 ? " disabled " : "") + "type='checkbox'  " + checkedStatus + " value='" + d.immediateOn + "' lay-skin='switch' lay-text='已开启|未开启' lay-filter='immediateOnOptFilter' data-id='" + d.id + "'>";
    };

    //立即只为首页
    form.on("switch(immediateOnOptFilter)", function (data) {
        let id = $(data.elem).attr("data-id");
        let status = 0;
        if (data.elem.checked) {
            status = 1;
        }
        let param = {id: id, immediateOn: status};
        CustomPage.updateBashInfo(param)
    });
    CustomPage.updateBashInfo = function (param) {
        $.ajax({
            url: Changein.ctxPath + '/customPage/updateBaseInfo',
            data: param,
            type: "POST",
            success: function (result) {
                if (result && result.code == 200) {
                    Changein.success("操作成功！");
                    table.reload(CustomPage.tableId);
                } else {
                    Changein.error("操作失败！");
                }
            }, dataType: "json"
        });
    }

    /**
     * 获取查询参数
     */
    function getParam() {
        var queryData = {};
        var queryData = {};
        queryData['pageName'] = $('#pageName').val();
        queryData['pageType'] = $('#pageType').val();
        return queryData;
    }


    /**
     * 跳转到添加页面
     */
    CustomPage.jumpAddPage = function () {
        window.location.href = Changein.ctxPath + '/customPage/add'
    };

    /**
     * 跳转到编辑页面
     *
     * @param data 点击按钮时候的行数据
     */
    CustomPage.jumpEditPage = function (data) {
        window.location.href = Changein.ctxPath + '/customPage/add?id=' + data.id
    };

    /**
     * 删除
     */
    var deleteBatchLock = false;
    CustomPage.deleteBatch = function () {
        if (!deleteBatchLock) {
            var checkRows = table.checkStatus(CustomPage.tableId);
            if (checkRows.data.length == 0) {
                Changein.error("请选择要删除的数据");
                return false;
            }
            var arr = [];
            var tabData = checkRows.data;
            for (var i = 0; i < tabData.length; i++) {
                if (tabData[i].audit != 1) {
                    arr.push(tabData[i].id);
                }
            }
            if (arr.length == 0) {
                Changein.error("选中数据全部已审核，请选择未审核的数据");
                return false;
            }
            var operation = function () {
                deleteBatchLock = true;
                var ajax = new $ax(Changein.ctxPath + "/customPage/batchDeleteByIds", function (data) {
                    if (data.code != 200) {
                        Changein.error("删除失败！" + data.message);
                    } else {
                        Changein.success("删除成功！");
                    }
                    deleteBatchLock = false;
                    table.reload(CustomPage.tableId);
                }, function (data) {
                    deleteBatchLock = false;
                    Changein.error("删除失败！" + data.responseJSON.message)
                });
                ajax.set({"ids": arr.toString()});
                ajax.start();
            }
            Changein.confirm("是否删除?", operation);
        } else {
            Changein.error("正在删除中");
        }
    };

    /**
     * 审核
     */
    var auditLock = false;
    CustomPage.auditBatch = function () {
        if (!auditLock) {
            var arr = [];
            var checkRows = table.checkStatus(CustomPage.tableId);
            if (checkRows.data.length == 0) {
                Changein.error("请选择要审核的数据");
                return false;
            }
            var tabData = checkRows.data;
            for (var i = 0; i < tabData.length; i++) {
                if (tabData[i].audit != 1) {
                    arr.push(tabData[i].id);
                }
            }
            if (arr.length == 0) {
                Changein.error("选中数据全部已审核");
                return false;
            }
            var operation = function () {
                auditLock = true;
                var ajax = new $ax(Changein.ctxPath + "/customPage/setAudit", function (data) {
                    if (data.code != 200) {
                        Changein.error("审核失败！" + data.message);
                    } else {
                        Changein.success("审核成功！");
                    }
                    auditLock = false;
                    table.reload(CustomPage.tableId);
                }, function (data) {
                    auditLock = false;
                    Changein.error("审核失败！" + data.responseJSON.message)
                });
                ajax.set({"id": arr.toString()});
                ajax.start();
            }
            Changein.confirm("是否审核?", operation);
        } else {
            Changein.error("正在审核中");
        }
    };

    /**
     * 反审核
     */
    var unAuditLock = false;
    CustomPage.unAuditBatch = function () {
        if (!unAuditLock) {
            var arr = [];
            var checkRows = table.checkStatus(CustomPage.tableId);
            if (checkRows.data.length == 0) {
                Changein.error("请选择要反审核的数据");
                return false;
            }
            var tabData = checkRows.data;
            for (var i = 0; i < tabData.length; i++) {
                if (tabData[i].audit == 1) {
                    arr.push(tabData[i].id);
                }
            }
            if (arr.length == 0) {
                Changein.error("选中数据全部未审核");
                return false;
            }
            var operation = function () {
                unAuditLock = true;
                var ajax = new $ax(Changein.ctxPath + "/customPage/setUnAudit", function (data) {
                    if (data.code != 200) {
                        Changein.error("反审核失败！" + data.message);
                    } else {
                        Changein.success("反审核成功！");
                    }
                    unAuditLock = false;
                    table.reload(CustomPage.tableId);
                }, function (data) {
                    unAuditLock = false;
                    Changein.error("反审核失败！" + data.responseJSON.message)
                });
                ajax.set({"id": arr.toString()});
                ajax.start();
            }
            Changein.confirm("是否反审核?", operation);
        } else {
            Changein.error("正在反审核中");
        }
    };


    /**
     * 导出excel按钮,EXPORT_CODE需要在开发设置-导出设置里配置
     */
    CustomPage.exportExcel = function () {
        Changein.exportData(tableResult.config.id, "EXPORT_CODE", table, getParam());
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    CustomPage.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Changein.ctxPath + "/customPage/delete", function (data) {
                Changein.success("删除成功!");
                table.reload(CustomPage.tableId);
            }, function (data) {
                Changein.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Changein.confirm("是否删除?", operation);
    };
    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    var deleteLock = false;
    CustomPage.onDeleteItem = function (data) {
        if (data.audit == 1) {
            Changein.error("选中数据已审核，请选择未审核的数据");
            return false;
        }
        var operation = function () {
            deleteLock = true;
            var ajax = new $ax(Changein.ctxPath + "/customPage/delete", function (data) {
                deleteLock = false;
                if (data.code != 200) {
                    Changein.error("删除失败！" + data.message);
                } else {
                    Changein.success("删除成功！");
                }
                table.reload(CustomPage.tableId);
            }, function (data) {
                deleteLock = false;
                Changein.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        if (!deleteLock) {
            Changein.confirm("是否删除?", operation);
        } else {
            Changein.error("正在删除中");
        }
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + CustomPage.tableId,
        url: Changein.ctxPath + '/customPage/list',
        page: true,
        height: "full-130",
        cellMinWidth: 100,
        cols: CustomPage.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        CustomPage.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        CustomPage.jumpAddPage();
    });
    // 删除
    $("#btnDel").click(function () {
        CustomPage.deleteBatch();
    });

    //审核
    $('#btnAudit').click(function () {
        CustomPage.auditBatch();
    });

    //反审核
    $('#btnUnAudit').click(function () {
        CustomPage.unAuditBatch();
    });
    // 导出excel
    $('#btnExp').click(function () {
        var checkRows = table.checkStatus(CustomPage.tableId);
        if (checkRows.data.length === 0) {
            CustomPage.exportExcel();
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }

    });
    //下载模板,TEMPLATE_CODE需要在开发设置-模板管理里配置
    $("#btnDownloadTemplate").click(function () {
        Changein.downLoadTempate("TEMPLATE_CODE");
    });
    // 工具条点击事件
    table.on('tool(' + CustomPage.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            CustomPage.jumpEditPage(data);
        } else if (layEvent === 'delete') {
            CustomPage.onDeleteItem(data);
        } else if (layEvent === 'setIndex') {
            let param = {id: data.id, pageType: 1}
            CustomPage.updateBashInfo(param)
        } else if (layEvent === 'cancelIndex') {
            let param = {id: data.id, pageType: 2}
            CustomPage.updateBashInfo(param)
        }
    });
});
